package com.wellgoon.test.exam;

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串最后一个单词的长度
 */
public class HJ1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入小于5000个字符的字符串");
        while (sc.hasNext()) {
            String str = sc.nextLine();
            if (null == str || str.length() == 0) {
                System.out.println("请输入小于5000个字符的字符串");
                continue;
            }
            /*if (!str.matches("[a-zA-Z ,.-]+$")){
                System.err.println("输入不合规：请输入小于5000个字符的英文单词字符串");
                continue;
            }*/

            if (str.length() >= 5000) {
                System.err.println("输入字符串长度超过5000个字符,请重新输入小于5000个字符的字符串");
                continue;
            }
            String lastWord = str.trim().substring(str.trim().lastIndexOf(" ") + 1);
            Pattern p = Pattern.compile("\\p{P}", Pattern.CASE_INSENSITIVE);
            char[] chars = lastWord.toCharArray();
            int count = 0;
            for (int i = 0; i < chars.length; i++) {
                Matcher m = p.matcher(chars[i] + "");
                while (m.find()) {
                    count++;
                }
            }

            System.out.println("最后一个单词[" + lastWord + "]长度为：" + (lastWord.length() - count));
        }
    }
}
